<template>
  <div>
    <el-card>
      <div v-show="scene == 0">
        <el-button type="primary" icon="el-icon-plus" @click="changeScene"
          >游戏上新</el-button
        >
        <el-table style="width: 100%" :data="gameList">
          <el-table-column type="index" label="序号" width="80" align="center">
          </el-table-column>
          <el-table-column
            prop="name"
            label="游戏名称"
            width="100px"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="gametype"
            label="游戏类别"
            width="80"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="page"
            label="游戏图片"
            width="width"
            align="center"
          >
            <template slot-scope="{ row, $index }">
              <img :src="row.page" style="width: 100px; height: 100px" />
            </template>
          </el-table-column>
          <el-table-column
            prop="introduce"
            label="游戏描述"
            width="400px"
            align="center"
          >
            <template slot-scope="scope">
              <el-tooltip :content="scope.row.introduce" placement="top">
                <p class="showOverTooltip">{{ scope.row.introduce }}</p>
              </el-tooltip>
            </template>
          </el-table-column>
          <el-table-column
            prop="previousmoney"
            label="原价"
            width="width"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="nowmoney"
            label="折扣价"
            width="width"
            align="center"
          >
          </el-table-column>
          <el-table-column
            prop="prop"
            label="折扣"
            width="80"
            align="center"
          >
           <template slot-scope="{ row, $index }"> <el-tag type="success" v-show="row.nowmoney!=row.previousmoney">已打折</el-tag></template>
         
          </el-table-column>
          <el-table-column
            prop="prop"
            label="操作"
            width="350px"
            align="center"
          >
            <template slot-scope="{ row, $index }">
              <HintButton
                type="primary"
                icon="el-icon-edit"
                title="添加折扣"
                slot="reference"
                :disabled="row.nowmoney!=row.previousmoney"
                @click="addDiscount(row)"
              ></HintButton>
              <HintButton
                type="info"
                icon="el-icon-info"
                title="下架折扣"
                slot="reference"
                :disabled="row.nowmoney==row.previousmoney"
                @click="deleteDiscount(row)"
              ></HintButton>
              <HintButton
                type="danger"
                icon="el-icon-delete"
                title="下架游戏"
                slot="reference"
                @click="deleteGame(row)"
              ></HintButton>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <AddGame
        v-show="scene == 1"
        ref="addgame"
        @changeTable="changeTable(0)"
      ></AddGame>
    </el-card>

    <el-dialog title="添加折扣" :visible.sync="dialogFormVisible">
      <el-form style="width: 80%" :model="discountForm">
        <el-form-item
          prop="discount_content"
          label="折扣活动"
          label-width="100px"
        >
          <el-input
            autocomplete="off"
            v-model="discountForm.discount_content"
          ></el-input>
        </el-form-item>
        <el-form-item label="打折力度">
          <div class="block">
            <span class="demonstration">（数字5:打五折）</span></span>
            <el-slider
              v-model="discountForm.discount_num"
              :format-tooltip="formatTooltip"
            ></el-slider>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submmitDiscount">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import AddGame from "@/views/games/AddGame";

export default {
  name: "Games",
  components: {
    AddGame,

  },
  data() {
    return {
      isShowTable: true,
      scene: 0,
      gameList: [],
      dialogFormVisible: false,
      discountForm: {gamename:'',discount_content:'',discount_num:0,},
    };
  },
  mounted() {
    this.getGameList();
  },
  methods: {
    // 获取全部游戏
    async getGameList() {
      let res = await this.$API.game.reqGameList();

      if (res.code == 200) {
        this.gameList = res.data;
      }
    },
    // 转去添加游戏
    changeScene() {
      this.scene = 1;
    },
    // 返回主界面
    changeTable(index) {
      this.scene = 0;
      this.getGameList();
    },
    // 下架游戏
    async deleteGame(row) {
      let name = row.name;
      let res = await this.$API.game.reqDeleteGame({ name: name });
      if (res.code == 200) {
        this.$message.success("下架成功");
        this.getGameList();
      }
    },
    // 添加游戏折扣
    addDiscount(row) {
      this.dialogFormVisible = true;
      this.discountForm.gamename=row.name;
    },
     // 格式化折扣力度
    formatTooltip(val) {
      return val / 10;
    },
    // 添加折扣
    async submmitDiscount(){
      let gamename = this.discountForm.gamename;
      let discount_content =this.discountForm.discount_content;
      let res = await this.$API.game.reqAddDisCount({
        gamename,
        discount_content,
        discount_num:this.discountForm.discount_num / 100
      })

      if(res.code==200){
        this.dialogFormVisible = false;
        this.$message.success('折扣添加成功')
        this.getGameList();
        this.discountForm={};
  
      }else{
        this.$message.danger('请不要重复添加')
        this.dialogFormVisible = false;
      }
    },
    // 下架折扣
    deleteDiscount(row){
      this.$confirm(`你确定删除${row.name}的折扣吗?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let result = await this.$API.game.reqDeleteDiscount({
            gamename: row.name,
          });
          if (result.code == 200) {
            this.$message({
              type: "success",
              message: "恢复原价成功",
            });
            this.getGameList();
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    
    }
  },
};
</script>

<style>
.showOverTooltip {
  display: -webkit-box;
  text-overflow: ellipsis;
  overflow: hidden;
  /*这里是3行*/
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
}
</style>
